from paver.easy import *
from paver.setuputils import setup
from setuptools import find_packages
import paver.doctools
import sys
import os

SRC_DIR = './src'
TEST_DIR = './tests'
BOOTSTRAP_DIR = "bootstrap"
BOOTSTRAP_PYEXEC = "%s/bin/python" % BOOTSTRAP_DIR
BOOTSTRAP_SCRIPT = "%s/bootstrap.py" % BOOTSTRAP_DIR

sys.path.extend(map(os.path.abspath, [SRC_DIR, TEST_DIR]))
import hornet.release as release

setup(
    name = release.name,
    version = release.version,
    author = release.author, 
    author_email = release.author_email,
    url = release.url,
    download_url = release.download_url,
    license = release.license,
    packages = find_packages(SRC_DIR),
    package_dir = {'': SRC_DIR},
    package_data = {
      '': ['logging.conf']
    },
    classifiers = [
      'License :: OSI Approved :: Apache Software License',
    ],
    zip_safe = True,

    entry_points = {
      'console_scripts': [
        'hornet = hornet.command:main',
      ],
    },

    #test_suite = 'nose.collector',
    #install_requires = ['matplotlib', 'numpy', 'decorator',
    #                    'networkx', 'sqlalchemy'], #'wxPython', 'configobj', 
    #setup_requires = ['sphinx==0.6.1', 'mock==0.5.0', 'nose=0.10.4', 'NoseXUnit==0.3.2', 'coverage==2.85'],
)

options(
    sphinx = Bunch(
        builddir = '.build'
    ),
    pylint = Bunch(
        module = ['hornet']
    ),
    htmlserve = Bunch(
        docsdir = './docs/.build/html/',
        port = 8000
    ),
    virtualenv=Bunch(
        script_name = BOOTSTRAP_SCRIPT,
        packages_to_install = ['sphinx==0.6.1', 'mock==0.5.0', 'nose=0.10.4', 
                               'NoseXUnit==0.3.2', 'coverage==2.85', 'networkx', 
                               'sqlalchemy==0.5.3', 'numpy==1.3.0'], 
                               #FIXME get matplotlib installed 'matplotlib==0.91.1'
    )
)

@task
def clean():
    path('build').rmtree()

@task
def bootstrap():
    """create virtualenv in ./bootstrap"""
    install = paver.path.path(BOOTSTRAP_DIR)
    if not install.exists():
        install.mkdir()
    call_task('paver.virtual.bootstrap')
    sh('cd %s; %s bootstrap.py' % (BOOTSTRAP_DIR, sys.executable))

@task
@needs(['paver.doctools.html'])
def html():
    """Build the docs."""
    pass

@task
@needs(['generate_setup', 'minilib', 'setuptools.command.sdist'])
def sdist():
    """Overrides sdist to make sure that our setup.py is generated."""
    pass

@task
@needs('html')
def doctest():
    """Test the doctests in Sphinx."""
    import sphinx
    options.order('sphinx', add_rest=True)
    paths = paver.doctools._get_paths()
    sphinxopts = ['', '-b', 'doctest', '-d', paths.doctrees, 
                  paths.srcdir, paths.htmldir]
    dry('sphinx-build %s' % (" ".join(sphinxopts),), sphinx.main, sphinxopts)

@task
@needs('doctest')
def snapshot():
    """Should be called on the webserver to update to the lastest documentation
    and releases.
    """
    directory = '/var/www/www.hiplab.org/projects/hornet/snapshot/docs'
    destdir = path(directory)
    destdir.rmtree()
    builtdocs = path('docs') / options.builddir / 'html'
    builtdocs.move(destdir)
    sh('chmod -R 755 %s' % directory)

@task
def sloccount():
    """Generate statistics from the sloccount package.Writes the output of 
    sloccount to sloccount.sc.  Expects that sloccount is installed on 
    the machine. Also outputs the general sloccount results to the screen.
    """
    sh('sloccount --wide --details src tests > sloccount.sc')
    sh('sloccount src tests')

@task
def pylint():
    """Check the module you're building with pylint."""
    from pylint import lint
    options.order('pylint', add_rest=True)
    pylintopts = options.module
    dry('pylint %s' % (" ".join(pylintopts)), lint.Run, pylintopts)

@task
def test():
    """Run the test suite."""
    import run_tests
    run_tests.main()
    
@task
def xmltest():
    """Run the test suite and output JUnit format results."""
    import run_tests
    run_tests.main(argv=['--with-nosexunit', '--enable-cover', '--source-folder=%s' % SRC_DIR])

@task
@needs('html')
def htmlserve():
    import SimpleHTTPServer
    import SocketServer
    import os
    
    os.chdir(options.docsdir)
    
    handler = SimpleHTTPServer.SimpleHTTPRequestHandler
    httpd = SocketServer.TCPServer(('', options.port), handler)
    
    print 'serving at port', options.port
    httpd.serve_forever()


@task
def tag():
    sh('hg tag v%s' % release.version)
    
    
